// components/major/major.js
const {addSpecialty}=require('../../api/api')
Component({
  /**
   * 组件的属性列表
   */
  properties: {
    list: {
      type: Array,
      value: []
    },
    width: {
      type: Number,
      value: 650
    },
    props: {
      type: Object,
      value: {
        name: 'name',
        code: 'code',
      }
    },
    style: {
      type: Object,
      value: {}
    },
    rowGap: {
      type: String,
      value: ''
    },
    mode: {
      type: String,
      value: ''
    }
  },

  /**
   * 组件的初始数据
   */
  data: {
    bottomLift: getApp().globalData.bottomLift,
    isVip:false
  },
  async attached (){
   const {vipType}= await getApp().fetchUserInfo()
     this.setData({
        isVip:vipType,
    }) 
 },
  /**
   * 组件的方法列表
   */
  methods: {
    goto(e) {
      const {code,id} = e.currentTarget.dataset
      wx.navigateTo({
        url: '/pages/major/detail/index?code=' + code,
      })
    },
    // 院校收藏
    async collect(e) {
      const {
        code,
        index,
        pindex,
        iscollect
      } = e.currentTarget.dataset

      const {
        list
      } = this.data
      list[pindex][index].isCollection = !iscollect
      const {
        result
      } = await addSpecialty(code)
      if (iscollect) {
        wx.showToast({
          title: '取消收藏',
          icon: 'none'
        })
      } else {
        wx.showToast({
          title: '收藏成功',
          icon: 'none'
        })
      }
      const params = {
        list: this.data.list,
        pindex,
        index
      }
      this.triggerEvent("collect", params);
    }
  }
})